extends Control
@onready var input_line_edit: LineEdit = %InputLineEdit
@onready var scroll_vbox_container: VBoxContainer = %ScrollVboxContainer
const SCHEDULE_ELEMENT = preload("res://schedule_element.tscn")
@onready var v_box_container: VBoxContainer = $VBoxContainer
@onready var progress_bar: ProgressBar = %ProgressBar
@export var save_path: String = "user://savegame.tres"
@onready var input_load_progress_bar: ProgressBar = %InputLoadProgressBar

func _ready() -> void:
	var use_data := save_path
	FileAccess.open(use_data, FileAccess.READ)
	if (not FileAccess.get_open_error()):
		load_schedules()


#region 提交和增加元素
# 提交文本
func _on_input_line_edit_text_submitted(new_text: String) -> void:
	if (new_text == ""):
		return
	# 调用接口
	$TestAPI.GetAnswer(new_text)
	%InputLineEdit.visible = false
	input_load_progress_bar.visible = true
	await $TestAPI.answer_finished
	input_load_progress_bar.visible = false
	%InputLineEdit.visible = true
	var answers: Array = get_ans_strings($TestAPI.new_answer)
	for element_string in answers:
		add_element(element_string[0], element_string[1], element_string[2])
	input_line_edit.clear()

# 答案格式: [12:10~12:20] - [吃饭] - [老食堂三楼]
func get_ans_strings(ans: String) -> Array:
	var regex = RegEx.new()
	regex.compile("\\[(\\d{2}:\\d{2}~\\d{2}:\\d{2})\\] - \\[(.+?)\\] - \\[(.+)\\]")
	var results = regex.search_all(ans)
	var time_scale: String
	var thing: String
	var place: String
	var elements_arr = []
	for res in results:
		time_scale = res.get_string(1)
		thing = res.get_string(2)
		place = res.get_string(3)
		var element_strings = [time_scale, thing, place]
		elements_arr.append(element_strings)
	return elements_arr

# 添加一个元素 
func add_element(time: String, thing: String, place: String):
	var new_schedule = SCHEDULE_ELEMENT.instantiate()
	new_schedule.update_elements(time, thing, place)
	scroll_vbox_container.add_child(new_schedule)
#endregion

#region 存档系统
func save_schedules():
	var save_game: SaveGame = SaveGame.new()
	var schedule_strings = get_schedule_strings()
	save_game.schedule_strings = schedule_strings
	var if_ok = ResourceSaver.save(save_game, save_path)

# 获取列表中的元素信息
func get_schedule_strings() -> Array:
	# 扫描当前的元素信息
	var schedule_strings = []
	for schedule in scroll_vbox_container.get_children():
		if (not schedule is ScheduleElement):
			continue
		var element_string = [schedule.time_text, schedule.thing_text, schedule.place_text]
		schedule_strings.append(element_string)
	return schedule_strings

# 加载存档
func load_schedules():
	var save_game:SaveGame = load(save_path)	
	var saving_schedules := save_game.schedule_strings
	for element_strings:Array in saving_schedules:
		add_element(element_strings[0], element_strings[1], element_strings[2])
#endregion

#region 调试使用
# 手动保存按钮
func _on_button_pressed() -> void:
	save_schedules()

func _on_load_button_pressed() -> void:
	load_schedules()

func _on_save_timer_timeout() -> void:
	save_schedules()

# 关闭按钮按下时
func _on_exit_button_pressed() -> void:
	save_schedules()
	# var tw = play_exit_bar_animation()
	# await tw.finished
	get_tree().quit()
#endregion

func play_exit_bar_animation() -> Tween:
	progress_bar.visible = true
	progress_bar.scale =  Vector2(1, 0)
	var tween := get_tree().create_tween()
	tween.set_trans(Tween.TRANS_QUINT)
	tween.tween_property(progress_bar, "value", 100, 1).set_trans(Tween.TRANS_QUINT)
	return tween
